<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="title" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="carCard" label-width="100px" :model="cardInfo" :rules="rules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="cardInfo.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="cardInfo.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="cardInfo.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="cardInfo.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="carPay" label-width="100px" :model="cardInfo" :rules="rules">
            <el-form-item label="有效日期" prop="cardStartDate">
              <el-date-picker
                v-model="dateRange"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="cardInfo.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="cardInfo.paymentMethod">
                <el-option
                  v-for="item in mapMethod"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetBtn">重置</el-button>
        <el-button type="primary" @click="confirmBtn">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCarCard, editCarCard, getCarCardDetail } from '@/api/carCard'

export default {
  name: 'AddMothCard',
  data() {
    return {
      cardInfo: {
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: '',
        cardStartDate: '',
        cardEndDate: '',
        paymentAmount: '',
        paymentMethod: 'Alipay'
      },
      rules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/, message: '请输入正确的车牌号码', trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' }
        ],
        cardStartDate: [
          { required: true, message: '请输入有效日期', trigger: 'blur' }
        ],
        cardEndDate: [
          { required: true, message: '请输入结束日期', trigger: 'blur' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请输入支付方式', trigger: 'blur' }

        ]

      },
      mapMethod: [
        { industryCode: 'Alipay', industryName: '支付宝' },
        { industryCode: 'WeChat', industryName: '微信' },
        { industryCode: 'Cash', industryName: '现金' }
      ],
      id: this.$route.params.id || ''
    }
  },
  computed: {
    dateRange: {
      get() {
        return [this.cardInfo.cardStartDate, this.cardInfo.cardEndDate]
      },
      set(range) {
        if (range && range.length === 2) {
          this.cardInfo.cardStartDate = range[0]
          this.cardInfo.cardEndDate = range[1]
        }
      }
    },
    title() {
      if (this.id) {
        return '编辑月卡'
      } else {
        return '增加月卡'
      }
    }
  },
  created() {
    if (this.id) {
      this.getCardInfo()
    }
  },
  methods: {
    async confirmBtn() {
      await this.$refs.carCard.validate()
      await this.$refs.carPay.validate()
      if (this.id) {
        // 编辑月卡
        await editCarCard(this.cardInfo)
        this.$router.push('/parking/card')
      } else {
        // 添加月卡
        await addCarCard(this.cardInfo)
        this.$router.push('/parking/card')
        this.resetBtn()
      }
    },
    // 获取编辑月卡信息
    async getCardInfo() {
      const res = await getCarCardDetail(this.id)
      // 解构赋值，排除 cardStatus
      // eslint-disable-next-line no-unused-vars
      const { cardStatus, ...restData } = res.data
      this.cardInfo = restData
    },
    resetBtn() {
      this.$refs.carCard.resetFields()
      this.$refs.carPay.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
